﻿using System;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Business.BaseInfo;
using NanoERP.Model;
using NanoERP.Common;

namespace NanoERP.BaseInfo
{
    [Module("FrmAccountSet")]
    public partial class FrmAccountSet : ModuleForm
    {
        readonly SysdictableBLL _sysdictableBLL;
        /// <summary>
        /// 是否已启用账套
        /// </summary>
        readonly Sysdictable _sysdictable1;
        /// <summary>
        /// 启用账套日期
        /// </summary>
        readonly Sysdictable _sysdictable2;
        public FrmAccountSet()
        {
            InitializeComponent();

            _sysdictableBLL = new SysdictableBLL();

            _sysdictable1 = _sysdictableBLL.Get((int)SysDicEnum.是否已启用账套);
            _sysdictable2 = _sysdictableBLL.Get((int)SysDicEnum.启用账套日期);

            dtTime.Value = DateTime.Parse(_sysdictable2.Value);
            SetControlEnable(_sysdictable1.Value.Equals("False"));
        }

        void SetControlEnable(bool isEnable)
        {
            dtTime.Enabled = btnAccount.Enabled = isEnable;
            btnbackAcc.Enabled = !isEnable;
        }

        private void btnAccount_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定要开启帐套吗？（期初开账后期初值不能修改）", "提示消息", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk
               , MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _sysdictable1.Value = "True";
                _sysdictable2.Value = dtTime.Value.ToString();
                _sysdictableBLL.Edit(_sysdictable1);
                _sysdictableBLL.Edit(_sysdictable2);

                MessageBox.Show("启用帐套成功！", "提示消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                SetControlEnable(false);
            }
        }

        private void btnbackAcc_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定要对当前帐套进行反开账吗？", "提示消息", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk
                , MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _sysdictable1.Value = "False";
                _sysdictableBLL.Edit(_sysdictable1);

                MessageBox.Show("反开账成功,可以录入和调整期初数据！", "提示消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                SetControlEnable(true);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
